<?php
/**
 * $Horde: groo/item.php,v 1.42.2.5 2006/01/01 21:29:05 jan Exp $
 *
 * Copyright 2002-2006 Chuck Hagenbuch <chuck@horde.org>
 *
 * See the enclosed file LICENSE for license information (ASL). If you
 * did not receive this file, see http://www.horde.org/licenses/asl.php.
 *
 * @author  Chuck Hagenbuch <chuck@horde.org>
 * @since   Groo 1.0
 * @package Groo
 */

@define('GROO_BASE', dirname(__FILE__));
require_once GROO_BASE . '/lib/base.php';
require_once 'Horde/Prefs/CategoryManager.php';
$cManager = &new Prefs_CategoryManager();

/* Redirect to the collection view if no action has been requested. */
$item_id = Util::getFormData('item');
$itemlist_id = Util::getFormData('itemlist');
$actionID = Util::getFormData('actionID');
if (is_null($actionID)) {
    header('Location: ' . Horde::applicationUrl('list.php', true));
    exit;
}

/* Run through the action handlers. */
switch ($actionID) {
case 'add_item':
    /* Check permissions. */
    if (Groo::hasPermission('max_items') !== true &&
        Groo::hasPermission('max_items') <= Groo::countItems()) {
        $message = @htmlspecialchars(sprintf(_("You are not allowed to create more than %d items."), Groo::hasPermission('max_items')), ENT_COMPAT, NLS::getCharset());
        if (!empty($conf['hooks']['permsdenied'])) {
            $message = Horde::callHook('_perms_hook_denied', array('groo:max_items'), 'horde', $message);
        }
        $notification->push($message, 'horde.error', array('content.raw'));
        header('Location: ' . Horde::applicationUrl('list.php', true));
        exit;
    }
    /* Set up the item attributes. */
    $itemlist_id = Groo::getDefaultCollection();
    if (is_a($itemlist_id, 'PEAR_Error')) {
        $notification->push($itemlist_id, 'horde.error');
    }
    $item_id = null;
    $item_body = '';
    $item_category = '';

    $title = _("Adding A New Item");
    $notification->push('document.item.item_body.focus();', 'javascript');
    break;

case 'modify_item':
    /* Get the current item. */
    $item = Groo::getItem($itemlist_id, $item_id);
    if (!$item || !isset($item['item_id'])) {
        $notification->push(_("Item not found."), 'horde.error');
        header('Location: ' . Horde::applicationUrl('list.php', true));
        exit;
    }

    /* Set up the item attributes. */
    $item_body = $item['body'];
    $item_category = $item['category'];
    $title = sprintf(_("Modifying %s"), $item['desc']);
    $notification->push('document.item.item_body.focus();', 'javascript');
    break;

case 'save_item':
    /* Get the form values. */
    $item_id = Util::getFormData('item');
    $item_body = Util::getFormData('item_body');
    $item_category = Util::getFormData('item_category');
    $itemlist_original = Util::getFormData('itemlist_original');
    $collection_target = Util::getFormData('collection_target');

    $share = $GLOBALS['groo_shares']->getShare($collection_target);
    if (is_a($share, 'PEAR_Error')) {
        $notification->push(sprintf(_("Access denied saving item: %s"), $share->getMessage()), 'horde.error');
    } elseif (!$share->hasPermission(Auth::getAuth(), PERMS_EDIT)) {
        $notification->push(sprintf(_("Access denied saving item to %s."), $share->get('name')), 'horde.error');
    } else {
        if ($new_category = Util::getFormData('new_category')) {
            $new_category = $cManager->add($new_category);
            if ($new_category) {
                $item_category = $new_category;
            }
        }

        /* If $item_id is set, we're modifying an existing item.  Otherwise,
         * we're adding a new item with the provided attributes. */
        if (!empty($item_id) &&
            !is_a(Groo::getItem($itemlist_original, $item_id), 'PEAR_Error')) {
            $storage = &Groo_Driver::singleton($itemlist_original);
            $item_desc = $storage->getItemDescription($item_body);
            $result = $storage->modify($item_id, $item_desc, $item_body, $item_category);

            if (!is_a($result, 'PEAR_Error') &&
                $itemlist_original != $collection_target) {
                /* Moving the item to another collection. */
                $share = $GLOBALS['groo_shares']->getShare($itemlist_original);
                if (!is_a($share, 'PEAR_Error') &&
                    $share->hasPermission(Auth::getAuth(), PERMS_DELETE)) {
                    $share = $GLOBALS['groo_shares']->getShare($collection_target);
                    if (!is_a($share, 'PEAR_Error') &&
                        $share->hasPermission(Auth::getAuth(), PERMS_EDIT)) {
                        $result = $storage->move($item_id, $collection_target);
                    } else {
                        $notification->push(_("Access denied moving the item."), 'horde.error');
                    }
                } else {
                    $notification->push(_("Access denied moving the item."), 'horde.error');
                }
            }
        } else {
            /* Check permissions. */
            if (Groo::hasPermission('max_items') !== true &&
                Groo::hasPermission('max_items') <= Groo::countItems()) {
                header('Location: ' . Horde::applicationUrl('list.php', true));
                exit;
            }
            /* Creating a new item. */
            $storage = &Groo_Driver::singleton($collection_target);
            $item_desc = $storage->getItemDescription($item_body);
            $result = $item_id = $storage->add($item_desc, $item_body, $item_category);
        }

        /* Check our results. */
        if (is_a($result, 'PEAR_Error')) {
            $notification->push(sprintf(_("There was an error saving the item: %s"), $result->getMessage()), 'horde.warning');
        } else {
            $notification->push(sprintf(_("Successfully saved \"%s\"."), $item_desc), 'horde.success');
        }
    }

    /* Return to the collection view. */
    header('Location: ' . Horde::applicationUrl('list.php', true));
    exit;
    break;

case 'delete_items':
    /* Delete the item if we're provided with a valid item ID. */
    $item_id = Util::getFormData('item');
    $itemlist_id = Util::getFormData('itemlist');

    if (!is_null($item_id) && Groo::getItem($itemlist_id, $item_id)) {
        $share = $GLOBALS['groo_shares']->getShare($itemlist_id);
        if (!is_a($share, 'PEAR_Error') &&
            $share->hasPermission(Auth::getAuth(), PERMS_DELETE)) {
            $storage = &Groo_Driver::singleton($itemlist_id);
            $result = $storage->delete($item_id);

            if (is_a($result, 'PEAR_Error')) {
                $notification->push(sprintf(_("There was an error removing the item: %s"), $result->getMessage()), 'horde.warning');
            } else {
                $notification->push(_("The item was deleted."), 'horde.success');
            }
        } else {
            $notification->push(_("Access denied deleting item."), 'horde.warning');
        }
    }

    /* Return to the collection. */
    header('Location: ' . Horde::applicationUrl('list.php', true));
    exit;
    break;

default:
    header('Location: ' . Horde::applicationUrl('list.php', true));
    exit;
}

$notification->push('document.item.item_body.focus()', 'javascript');
$collections = Groo::listCollections(false, PERMS_EDIT);
require GROO_TEMPLATES . '/common-header.inc';
require GROO_TEMPLATES . '/menu.inc';
require GROO_TEMPLATES . '/item/item.inc';
require $registry->get('templates', 'horde') . '/common-footer.inc';
